package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.AddressBookAddDTO;
import com.sky.dto.AddressBookSetDefaultDTO;
import com.sky.dto.AddressBookUpdateDTO;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    /**
     * 查询收获地址
     * */
    @Override
    public List<AddressBook> list() {
        Long currentId = BaseContext.getCurrentId();
        List<AddressBook>list= addressBookMapper.selectByUserId(currentId);
        return list;




    }
    /**
     * 新增收货地址
     * */

    @Override
    public void add(AddressBookAddDTO addressBookAddDTO) {
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookAddDTO,addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.insert(addressBook);



    }
    /**
     * 编辑地址回显
     * */

    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook=  addressBookMapper.getById(id);
        return addressBook;


    }
    /**
     * 根据id修改收货地址
     * */

    @Override
    public void update(AddressBookUpdateDTO addressBookUpdateDTO) {
        //处理默认，现在改成默认，需要保证不能出现两个默认
        Integer isDefault = addressBookUpdateDTO.getIsDefault();
        //修改当前记录是默认
        if(isDefault!=null&& Objects.equals(isDefault,1)){
            //把之前的默认改为非默认
            addressBookMapper.updateNotDefaultByUsrId(BaseContext.getCurrentId());
            //设置当前的为默认
            AddressBook addressBook = new AddressBook();
            BeanUtils.copyProperties(addressBookUpdateDTO,addressBook);
            addressBook.setUserId(BaseContext.getCurrentId());
            addressBookMapper.update(addressBook);
        }else {//不是默认
            //根据id更新
            AddressBook addressBook = new AddressBook();
            BeanUtils.copyProperties(addressBookUpdateDTO,addressBook);
            addressBook.setUserId(BaseContext.getCurrentId());
            addressBookMapper.update(addressBook);
        }




    }
    /**
     * 修改默认地址
     * */

    @Override
    public void setDefault(AddressBookSetDefaultDTO dto) {
        //把之前的默认给取消掉
        addressBookMapper.updateNotDefaultByUsrId(BaseContext.getCurrentId());
        //设置当前的为默认
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(dto,addressBook);
        //addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        addressBook.setId(dto.getId());
        addressBookMapper.update(addressBook);

    }
    /**
     * 根据id删除地址
     * */

    @Override
    public void deleteById(Long id) {
        addressBookMapper.deleteById(id);
    }
}
